using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC.Xpo;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Xpo;
using FeatureCenter.Module;
using System.Configuration;

namespace FeatureCenter.Web
{
    public partial class FeatureCenterAspNetApplication : WebApplication
    {
        private DevExpress.ExpressApp.SystemModule.SystemModule module1;
        private DevExpress.ExpressApp.Web.SystemModule.SystemAspNetModule module2;
        private FeatureCenter.Module.FeatureCenterModule module3;
        private FeatureCenter.Module.Web.FeatureCenterAspNetModule module4;
		//private DevExpress.ExpressApp.Reports.Web.ReportsAspNetModule module5;
		private DevExpress.ExpressApp.Objects.BusinessClassLibraryCustomizationModule module6;
        private DevExpress.ExpressApp.FileAttachments.Web.FileAttachmentsAspNetModule module7;
        private DevExpress.ExpressApp.Validation.ValidationModule module8;
		private DevExpress.ExpressApp.HtmlPropertyEditor.Web.HtmlPropertyEditorAspNetModule module9;
		private DevExpress.ExpressApp.TreeListEditors.Web.TreeListEditorsAspNetModule module10;
		private DevExpress.ExpressApp.Scheduler.Web.SchedulerAspNetModule schedulerAspNetModule1;
		private DevExpress.ExpressApp.Scheduler.SchedulerModuleBase schedulerModuleBase1;
        private DevExpress.ExpressApp.ScriptRecorder.Web.ScriptRecorderAspNetModule scriptRecorderAspNetModule1;
        private DevExpress.ExpressApp.ScriptRecorder.ScriptRecorderModuleBase scriptRecorderModuleBase1;
        private DevExpress.ExpressApp.ViewVariantsModule.ViewVariantsModule viewVariantsModule1;
		private DevExpress.ExpressApp.ConditionalAppearance.ConditionalAppearanceModule conditionalAppearanceModule;
		private DevExpress.ExpressApp.Kpi.KpiModule kpiModule;
		private DevExpress.ExpressApp.Chart.ChartModule chartModule;
		private DevExpress.ExpressApp.Chart.Web.ChartAspNetModule chartAspNetModule;
		private DevExpress.ExpressApp.PivotGrid.PivotGridModule pivotGridModule;
		private DevExpress.ExpressApp.PivotGrid.Web.PivotGridAspNetModule pivotGridAspNetModule;
        private DevExpress.ExpressApp.StateMachine.StateMachineModule stateMachine1;
        private DevExpress.ExpressApp.Reports.ReportsModule reportsModule;
        private DevExpress.ExpressApp.Reports.Web.ReportsAspNetModule reportsAspNetModule;

        private static FeatureCenterXpoDataStoreProvider provider;
        private static readonly object CreateDefaultObjectSpaceProviderLockObject = new object();

        public FeatureCenterAspNetApplication() {
            InitializeComponent();
        }

        private void FeatureCenterAspNetApplication_DatabaseVersionMismatch(object sender, DevExpress.ExpressApp.DatabaseVersionMismatchEventArgs e) {
            e.Updater.Update();
            e.Handled = true;
        }

        protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
            if(provider == null) {
                lock(CreateDefaultObjectSpaceProviderLockObject) {
                    if(provider == null) {
                        try {
                            string connectionString = "Integrated Security=SSPI;Pooling=false;Data Source=(local);Initial Catalog=FeatureCenter_v13.2";
                            if(ConfigurationManager.AppSettings["SiteMode"] != null && ConfigurationManager.AppSettings["SiteMode"].ToLower() == "true") {
                                connectionString = InMemoryDataStoreProvider.ConnectionString;
                            }
                            else {
                                if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
                                    connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                                }
                            }


                            provider = new FeatureCenter.Module.FeatureCenterXpoDataStoreProvider();
                            provider.Initialize(
                                string.Format(@"XpoProvider=InMemoryDataStore; Read Only=True; Data Source={0}\App_Data\nwind.xml", AppDomain.CurrentDomain.SetupInformation.ApplicationBase),
                                connectionString);
                        }
                        catch {
                            provider = null;
                            throw;
                        }
                    }
                }
            }
            args.ObjectSpaceProvider = new XPObjectSpaceProvider(provider);
        }

        private void InitializeComponent()
        {
            this.module1 = new DevExpress.ExpressApp.SystemModule.SystemModule();
            this.module2 = new DevExpress.ExpressApp.Web.SystemModule.SystemAspNetModule();
            this.module3 = new FeatureCenter.Module.FeatureCenterModule();
            this.module4 = new FeatureCenter.Module.Web.FeatureCenterAspNetModule();
            this.module6 = new DevExpress.ExpressApp.Objects.BusinessClassLibraryCustomizationModule();
			//this.module5 = new DevExpress.ExpressApp.Reports.Web.ReportsAspNetModule();
            this.module7 = new DevExpress.ExpressApp.FileAttachments.Web.FileAttachmentsAspNetModule();
            this.module8 = new DevExpress.ExpressApp.Validation.ValidationModule();
			this.module9 = new DevExpress.ExpressApp.HtmlPropertyEditor.Web.HtmlPropertyEditorAspNetModule();
			this.module10 = new DevExpress.ExpressApp.TreeListEditors.Web.TreeListEditorsAspNetModule();
			this.schedulerAspNetModule1 = new DevExpress.ExpressApp.Scheduler.Web.SchedulerAspNetModule();
			this.schedulerModuleBase1 = new DevExpress.ExpressApp.Scheduler.SchedulerModuleBase();
            this.scriptRecorderModuleBase1 = new DevExpress.ExpressApp.ScriptRecorder.ScriptRecorderModuleBase();
            this.scriptRecorderAspNetModule1 = new DevExpress.ExpressApp.ScriptRecorder.Web.ScriptRecorderAspNetModule();
            this.viewVariantsModule1 = new DevExpress.ExpressApp.ViewVariantsModule.ViewVariantsModule();
			this.conditionalAppearanceModule = new DevExpress.ExpressApp.ConditionalAppearance.ConditionalAppearanceModule();
			this.kpiModule = new DevExpress.ExpressApp.Kpi.KpiModule();
			this.chartModule = new DevExpress.ExpressApp.Chart.ChartModule();
			this.chartAspNetModule = new DevExpress.ExpressApp.Chart.Web.ChartAspNetModule();
			this.pivotGridModule = new DevExpress.ExpressApp.PivotGrid.PivotGridModule();
			this.pivotGridAspNetModule = new DevExpress.ExpressApp.PivotGrid.Web.PivotGridAspNetModule();
            this.stateMachine1 = new DevExpress.ExpressApp.StateMachine.StateMachineModule();
            this.reportsModule = new DevExpress.ExpressApp.Reports.ReportsModule();
            this.reportsAspNetModule = new DevExpress.ExpressApp.Reports.Web.ReportsAspNetModule();
            
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            //
            // ReportsModule
            //
            this.reportsModule.ReportDataType = typeof(DevExpress.Persistent.BaseImpl.ReportData);
            // 
            // FeatureCenterAspNetApplication
            // 
            this.ApplicationName = "FeatureCenter";
            this.Modules.Add(this.module1);
            this.Modules.Add(this.module2);
            this.Modules.Add(this.viewVariantsModule1);
            this.Modules.Add(this.module3);
			this.Modules.Add(this.module4);
			//this.Modules.Add(this.module5);
			this.Modules.Add(this.module6);
			this.Modules.Add(this.module7);
            this.Modules.Add(this.module8);
			this.Modules.Add(this.module9);
			this.Modules.Add(this.module10);
			this.Modules.Add(this.schedulerModuleBase1);
			this.Modules.Add(this.schedulerAspNetModule1);
            this.Modules.Add(this.scriptRecorderModuleBase1);
            this.Modules.Add(this.scriptRecorderAspNetModule1);
			this.Modules.Add(this.conditionalAppearanceModule);
			this.Modules.Add(this.kpiModule);
			this.Modules.Add(this.chartModule);
			this.Modules.Add(this.chartAspNetModule);
			this.Modules.Add(this.pivotGridModule);
			this.Modules.Add(this.pivotGridAspNetModule);
            this.Modules.Add(this.stateMachine1);
            this.Modules.Add(this.reportsModule);
            this.Modules.Add(this.reportsAspNetModule);

			this.DatabaseVersionMismatch += new System.EventHandler<DevExpress.ExpressApp.DatabaseVersionMismatchEventArgs>(this.FeatureCenterAspNetApplication_DatabaseVersionMismatch);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
        }
    }
}
